//
//  SEGTarget_Web.m
//  Commom
//
//  Created by 山刘 on 2018/12/7.
//  Copyright © 2018年 山刘. All rights reserved.
//

#import "SEGTarget_Web.h"
#import "SEGUrlAddress.h"

#import <Commom/SEGHTTPManager.h>
#import <Commom/SEGAlertView.h>

#import "SEGWKWebViewLoadController.h"

@implementation SEGTarget_Web
/**
 * 创建网页
 * @author Helen
 * @param params linkURL  链接入口; webView  主视图webView; shouldResetNavgation  是否需要重新设置导航栏
 */
-(UIViewController *)segAction_initWebViewLoadController:(NSDictionary *)params{
    if (!(params && [params isKindOfClass:[NSDictionary class]] && [[params allKeys] containsObject:@"linkURL"])) return nil;
    
    //检测网络状态 没网的时候 不进行跳转
    SEGNetworkStatus status = [[SEGHTTPManager sharedNetworkManager] segNetworkStatus];
    if (status == SEGNetworkStatusNotReachable || status == SEGNetworkStatusUnknown) {
        [SEGAlertView showTmepMessageWithMessage:@"网络请求失败，请检查你的网络设置"];
        return nil;
    }
    
    // TODO: 替换平台执行
    //    if (YES) {
    SEGWKWebViewLoadController *webVC = [[SEGWKWebViewLoadController alloc] init];
    webVC.webType = [[params valueForKey:@"webType"] integerValue];
    if ([params valueForKey:@"dict"]) {
        webVC.tempDict = [params valueForKey:@"dict"];
    }
    
    if ([params valueForKey:@"completed"]) {
        webVC.completed = [params valueForKey:@"completed"];
    }
    //是否为根视图 tab
    if ([params valueForKey:@"isRootVC"]) {
        webVC.isRootVC = [params valueForKey:@"isRootVC"];
    }
    
    webVC.shouldResetNavgation = [[params valueForKey:@"shouldResetNavgation"] boolValue];
    
    // 单独处理数字社区的导航栏
    if([[params valueForKey:@"isDigitalComunity"] boolValue]){
        webVC.isDigitalComunity = YES;
    }
    
    [webVC segAddSubview];
    [webVC segAddKVO];
    [webVC segAddNotifications];
    webVC.linkURL = [webVC loadDataWithURLString:[params valueForKey:@"linkURL"]];
    return webVC;
}

/**
 * 创建网页
 * @author lima
 * @param params linkURL
 */
-(UIViewController *)segAction_initTabWebViewLoadController:(NSDictionary *)params {
    SEGWKWebViewLoadController *webVC = [[SEGWKWebViewLoadController alloc] init];
    webVC.webType = [[params valueForKey:@"webType"] integerValue];
    if ([params valueForKey:@"dict"]) {
        webVC.tempDict = [params valueForKey:@"dict"];
    }
    
    if ([params valueForKey:@"completed"]) {
        webVC.completed = [params valueForKey:@"completed"];
    }
    //是否为根视图 tab
    if ([params valueForKey:@"isRootVC"]) {
        webVC.isRootVC = [params valueForKey:@"isRootVC"];
    }
    
    webVC.shouldResetNavgation = [[params valueForKey:@"shouldResetNavgation"] boolValue];
    
    // 单独处理数字社区的导航栏
    if([[params valueForKey:@"isDigitalComunity"] boolValue]){
        webVC.isDigitalComunity = YES;
    }
    
    [webVC segAddSubview];
    [webVC segAddKVO];
    [webVC segAddNotifications];
    webVC.linkURL = [webVC loadDataWithURLString:[params valueForKey:@"linkURL"]];
    return webVC;
}

/**
 获取网页的Class

 @param params 参数
 @return Class
 */
-(Class)segAction_getWebViewLoadControllerClass:(NSDictionary *)params {
    return [SEGWKWebViewLoadController class];
}

/**
 重新加载网页数据

 @param params 参数
 */
-(void)segAction_reloadWebViewData:(NSDictionary *)params {
    SEGWKWebViewLoadController *vc = params[@"webVC"];
    if ([vc isKindOfClass:[SEGWKWebViewLoadController class]]) {
        [vc loadData];
    }
}

@end
